T = int(input())
anss = []
for _ in range(T):
n = int(input())
dic = {}
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [[] for i in range(n)]
for i in range(n):
c[i].append(a[i])
c[i].append(b[i])
ssum = 0
ans = int(1e9)
c.sort()
for i in range(n-1,-1,-1):
ans = min(ans , max(ssum,c[i][0]))
ssum += c[i][1]
anss.append(str(min(ans,ssum)))
print('\n'.join(anss))
#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define PI 3.14159
#define llinf LLONG_MAX
#define inf INT_MAX
#define _inf INT_MIN
#define _llinf LLONG_MIN
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--) {
ll n;
cin>>n;
vector<pair<ll, ll>> v(n);
for(int i=0; i<n; i++) {
cin>>v[i].first;
}
for(int i=0; i<n; i++) {
cin>>v[i].second;
}
sort(v.begin(), v.end());
ll suff[n];
suff[n-1] = v[n-1].second;
for(int i=n-2; i>=0; i--) {
suff[i] = suff[i+1] + v[i].second;
}
ll ans = min(suff[0], v[n-1].first);
for(int i=n-2; i>=0; i--) {
ll x = max(v[i].first, suff[i+1]);
ans = min(ans, x);
}
cout<<ans<<"\n";
// cout<<suff[0]<<"\n";
}
}
2148. Count Elements With Strictly Smaller and Greater Elements | 2149. Rearrange Array Elements by Sign |
2150. Find All Lonely Numbers in the Array | 2151. Maximum Good People Based on Statements |
2144. Minimum Cost of Buying Candies With Discount | Non empty subsets |
1630A - And Matching | 1630B - Range and Partition |
1630C - Paint the Middle | 1630D - Flipping Range |
1328A - Divisibility Problem | 339A - Helpful Maths |
4A - Watermelon | 476A - Dreamoon and Stairs |
1409A - Yet Another Two Integers Problem | 977A - Wrong Subtraction |
263A - Beautiful Matrix | 180C - Letter |
151A - Soft Drinking | 1352A - Sum of Round Numbers |
281A - Word Capitalization | 1646A - Square Counting |
266A - Stones on the Table | 61A - Ultra-Fast Mathematician |
148A - Insomnia cure | 1650A - Deletions of Two Adjacent Letters |
1512A - Spy Detected | 282A - Bit++ |
69A - Young Physicist | 1651A - Playoff |